package com.jixiang.tracedata.service;

import com.jixiang.tracedata.mapper.TraceOriginalMapper;
import com.jixiang.tracedata.mapper.TravellerMapper;
import com.jixiang.tracedata.model.common.Hdr;
import com.jixiang.tracedata.model.traveller.*;
import com.jixiang.tracedata.util.StringOperate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

/**
 * @author quanbiao
 * @date 2018-09-11
 */
@Service("travellerService")
public class TravellerService {

    @Autowired
    private TravellerMapper travellerMapper;

    @Autowired
    private TraceOriginalMapper traceOriginalMapper;

    @Transactional(rollbackFor = Exception.class)
    public void addTraceTraveller(String xmlContent, Msg msg){
        //插入旅客原始数据
        traceOriginalMapper.addTraceOriginalData(xmlContent);
        //插入旅客解析数据
        addPassengerMessage(msg);
        addTravellerDocument(msg);
        addAssociatedSegment(msg);
        addBaggage(msg);
    }


    /**
     * 旅客总体信息插入
     * @param msg
     * @return
     */
    public int addPassengerMessage(Msg msg){
        Dat dat = msg.getDat();
        if(null != dat){
            PassengerSegment passengerSegment = dat.getPassengerSegment();
            if(null != passengerSegment){
                Segment segment = passengerSegment.getSegment();
                if(null != segment){
                    List<SpecialServiceRequest> specialServiceRequestList = segment.getSpecialServiceRequestList();
                    if(null != specialServiceRequestList && specialServiceRequestList.size() > 0){
                        String specialServiceRequest = StringOperate.listObjectToString(specialServiceRequestList);
                        passengerSegment.setDB_SpecialServiceRequest(specialServiceRequest);
                    }
                    List<GeneralRemark> generalRemarkList = segment.getGeneralRemarkList();
                    if(null != generalRemarkList && generalRemarkList.size() > 0){
                        String generalRemark = StringOperate.listObjectToString(generalRemarkList);
                        passengerSegment.setDB_GeneralRemark(generalRemark);
                    }
                    List<OtherServiceInformation> otherServiceInformationList = segment.getOtherServiceInformationList();
                    if(null != otherServiceInformationList && otherServiceInformationList.size() > 0){
                        String otherService = StringOperate.listObjectToString(otherServiceInformationList);
                        passengerSegment.setDB_OtherServiceInformation(otherService);
                    }
                    List<FrequentTraveler> frequentTravelerList = segment.getFrequentTravelerList();
                    if(null != frequentTravelerList &&frequentTravelerList.size() > 0){
                        String frequentTraveler = StringOperate.listObjectToString(frequentTravelerList);
                        passengerSegment.setDB_FrequentTraveler(frequentTraveler);
                    }
                    List<Phone> phoneList = segment.getPhoneList();
                    if(null != phoneList && phoneList.size() > 0){
                        String phoneString = StringOperate.listObjectToString(phoneList);
                        passengerSegment.setDB_Phone(phoneString);
                    }
                    List<String> emailList = segment.getEmailList();
                    if(null != emailList && emailList.size() > 0){
                        String emailString = StringOperate.listStringToString(emailList, "Email");
                        passengerSegment.setDB_Email(emailString);
                    }
                    List<Contact> contactList = segment.getContactList();
                    if(null != contactList && contactList.size() > 0){
                        String contactString = StringOperate.listObjectToString(contactList);
                        passengerSegment.setDB_Contact(contactString);
                    }
                }

                BookingInfo bookingInfo = passengerSegment.getBookingInfo();
                if(null != bookingInfo){
                    List<String> formOfPaymentsList = bookingInfo.getFormOfPaymentsList();
                    if(null != null && formOfPaymentsList.size() > 0){
                        String formOfPayments = StringOperate.listStringToString(formOfPaymentsList, "FormOfPayments");
                        passengerSegment.setDB_FormOfPayments(formOfPayments);
                    }
                }
                TicketInfo ticketInfo = passengerSegment.getTicketInfo();
                if(null != ticketInfo){
                    FareGroup fareGroup = ticketInfo.getFareGroup();
                    if(null != fareGroup){
                        Taxes taxes = fareGroup.getTaxes();
                        if(null != taxes){
                            List<Tax> taxList = taxes.getTaxList();
                            if(null != taxList && taxList.size() > 0){
                                String tax = StringOperate.listObjectToString(taxList);
                                passengerSegment.setDB_Tax(tax);
                            }
                        }
                    }
                    List<String> fareCalculationList = ticketInfo.getFareCalculationList();
                    if(null != fareCalculationList && fareCalculationList.size() > 0){
                        String fareCalculation = StringOperate.listStringToString(fareCalculationList, "FareCalculation");
                        passengerSegment.setDB_FareCalculation(fareCalculation);
                    }
                    List<String> endorsementList = ticketInfo.getEndorsementList();
                    if(null != endorsementList && endorsementList.size() > 0){
                        String endorsement = StringOperate.listStringToString(endorsementList, "Endorsement");
                        passengerSegment.setDB_Endorsement(endorsement);
                    }
                }
                EMDTicketInfo emdTicketInfo = passengerSegment.getEMDTicketInfo();
                if(null != emdTicketInfo){
                    FareGroup fareGroup = emdTicketInfo.getFareGroup();
                    if(null != fareGroup){
                        List<TotalFare> totalFareList = fareGroup.getTotalFareList();
                        if(null != totalFareList && totalFareList.size() > 0){
                            String totalFate = StringOperate.listObjectToString(totalFareList);
                            passengerSegment.setDB_TotalFare(totalFate);
                        }
                        Taxes taxes = fareGroup.getTaxes();
                        if(null != taxes){
                            List<Tax> taxList = taxes.getTaxList();
                            if(null != taxList && taxList.size() > 0){
                                String tax = StringOperate.listObjectToString(taxList);
                                passengerSegment.setDB_EMDTaxesTax(tax);
                            }
                        }
                    }
                }
            }

        }
        return travellerMapper.addPassengerMessage(msg);
    }

    /**
     * 旅客Document数据插入
     * @param msg
     * @return
     */
    public int addTravellerDocument(Msg msg){
        int retInt = 0;
        Hdr hdr = msg.getHdr();
        Dat dat = msg.getDat();
        if(null != dat){
            PassengerSegment passengerSegment = dat.getPassengerSegment();
            if(null != passengerSegment){
                Traveller traveller = passengerSegment.getTraveller();
                if(null != traveller){
                    List<Document> documentList = traveller.getDocumentList();
                    if(null != documentList && documentList.size() > 0){
                        for (Document document : documentList) {
                            document.setHdr(hdr);
                        }
                        retInt = travellerMapper.addTravellerDocument(documentList);
                    }
                }
            }
        }
        return retInt;
    }

    /**
     * 旅客数据AssociatedSegment数据插入
     * @param msg
     * @return
     */
    public int addAssociatedSegment(Msg msg){
        int retInt = 0;
        Hdr hdr = msg.getHdr();
        Dat dat = msg.getDat();
        if(null != dat){
            PassengerSegment passengerSegment = dat.getPassengerSegment();
            if(null != passengerSegment){
                List<AssociatedSegment> associatedSegmentList = passengerSegment.getAssociatedSegmentList();
                if(null != associatedSegmentList && associatedSegmentList.size() > 0){
                    for (AssociatedSegment associatedSegment : associatedSegmentList) {
                        associatedSegment.setHdr(hdr);
                    }
                    retInt = travellerMapper.addAssociatedSegment(associatedSegmentList);
                }
            }
        }
        return retInt;
    }

    /**
     * 旅客数据Baggage数据插入
     * @param msg
     * @return
     */
    public int addBaggage(Msg msg){
        int retInt = 0;
        Hdr hdr = msg.getHdr();
        Dat dat = msg.getDat();
        if(null != dat){
            PassengerSegment passengerSegment = dat.getPassengerSegment();
            if(null != passengerSegment){
                DepartureInfo departureInfo = passengerSegment.getDepartureInfo();
                if(null != departureInfo){
                    BaggageInfo baggageInfo = departureInfo.getBaggageInfo();
                    if(null != baggageInfo){
                        List<Baggage> baggageList = baggageInfo.getBaggage();
                        if(null != baggageList && baggageList.size() > 0){
                            for (Baggage baggage : baggageList) {
                                baggage.setHdr(hdr);
                                List<Piece> pieceList = baggage.getPieceList();
                                if(null != pieceList && pieceList.size() > 0){
                                    String pieceStr = StringOperate.listObjectToString(pieceList);
                                    baggage.setDB_Piece(pieceStr);
                                }
                            }
                            retInt = travellerMapper.addBaggage(baggageList);
                        }
                    }
                }
            }
        }
        return retInt;
    }

}
